<?php
class ItemManagerController extends Zend_Controller_Action
{
    public function init ()
    {
        $uri = $this->_request->getPathInfo();
        $activeNav = $this->view->navigation()->findByHref($uri);
        if ($activeNav != null) {
            $activeNav->active = true;
            $activeNav->setClass("active");
        }
    }
    public function indexAction ()
    {
        $this->_redirect("/");
    }
    public function getitemsAction ()
    {
        $path = null;
        $post = $this->getRequest()->getPost();
        $cateNS = new Zend_Session_Namespace("Category");
        if (! empty($post["path"])) {
            $cateNS->curItem = $post["path"];
            $path = $post["path"];
        } else {
            if (! empty($cateNS->curItem)) {
                $path = $cateNS->curItem;
            } else {
                $this->_redirect("/");
            }
        }
        $page = $this->_getParam('page', 1);
        $options = Zend_Registry::get("sysOptions");
        $pagesize = $options["pagesize"];
        $start = $pagesize * ($page - 1);
        $itemPath = $path;
        $strArr = explode("?", $itemPath);
        $path = $strArr[0];
        $parameters = array("order_by" => "title", "start" => $start, 
        "limit" => $pagesize);
        $data = Application_Model_RESTWSManager::getData($path, $parameters);
        $total = $data["total"];
        if ($total > 0) {
            $paginator = Zend_Paginator::factory(1 * $total);
            $paginator->setItemCountPerPage($pagesize);
            $paginator->setCurrentPageNumber($page);
            $this->view->paginator = $paginator;
            $this->view->items = $total > 1 ? $data["items"]["item"] : $data["items"];
            $this->view->total = $total;
        }
        $viewStore=new Application_Model_ViewStore("ItemManager", "getitems",null,$page);
        $viewNs=new Zend_Session_Namespace("viewNs");
        $viewNs->levelTwoView=$viewStore;
    }
    public function getitemAction ()
    {
        $post = $this->getRequest()->getPost();
        $itemNS = new Zend_Session_Namespace("Item");
            if (! empty($post["path"])) {
            $itemNS->curItem = $post["path"];
            $itemPath = $post["path"];
        } else {
            if (! empty($itemNS->curItem)) {
                $itemPath = $itemNS->curItem;
            } else {
                $this->_redirect("/");
            }
        }
       // $itemPath = $post["path"];
        $strArr = explode("?", $itemPath);
        $path = $strArr[0];
        $parameters = array("all_fields" => "1");
        $e = Application_Model_RESTWSManager::getData($path, $parameters);
        $item = $e["item"];
        if (key_exists("item_uri", $item)) {
            unset($item["item_uri"]);
        }
        if (key_exists("multimedia_uri", $item)) {
            $pathStr = $item["multimedia_uri"];
            $mediaStrArr = explode("?", $pathStr);
            $mediapath = $mediaStrArr[0];
            $d = Application_Model_RESTWSManager::getData($mediapath);
            $mediaTotal = $d["total"];
            if ($mediaTotal > 0) {
                $multimedias =$d["multimedia"]["multimedia"];
                $this->view->multimedias = $mediaTotal > 1 ? $multimedias : array(
                $multimedias);
            }
            unset($item["multimedia_uri"]);
        }
        $this->view->item = $item;
    }

    public function donateAction ()
    {}
    public function creditcardcheckAction ()
    {
    	if(empty($_POST)||!key_exists("wslocation", $_POST)){
    		$this->_redirect("/");
    	}
        $wstype = $_POST["wslocation"];
        $parameters = array("cardName" => $_POST["cardName"], 
        "cardAmount" => $_POST["cardAmount"], "cardNumber" => $_POST["cardNumber"], 
        "cardType" => $_POST["cardType"]);
        $parameters["expdate"] = $_POST["month"] . "/" . $_POST["year"];
//        $request = new Application_Model_CheckCardRequest($parameters);
        $message = Application_Model_SOAPWSManager::ccCheck($wstype, $parameters);
        $this->view->message = $message;
    }
}



